package com.zzn.bookManage.service.impl;

import com.zzn.bookManage.dao.BookDao;
import com.zzn.bookManage.dao.impl.BookDaoImpl;
import com.zzn.bookManage.pojo.Book;
import com.zzn.bookManage.pojo.ResultEntity;
import com.zzn.bookManage.service.BookService;
import com.zzn.bookManage.util.StringUtil;

import java.util.List;
import java.util.Map;

public class BookServiceImpl implements BookService {

    private static final BookDao bookDao = new BookDaoImpl();

    @Override
    public boolean addBook(Book in) {
        // 非空校验
        if (in == null
        ) {
            return false;
        }
        return bookDao.addBook(in);
    }

    @Override
    public boolean updateBook(Book in) {
        if (in == null || in.getBid() == null
        ) {
            return false;
        }
        return bookDao.updateBook(in);
    }

    @Override
    public boolean deleteBook(Book in) {
        if (in == null || in.getBid() == null) {
            return false;
        }
        return bookDao.deleteBook(in);
    }

    @Override
    public boolean returnBook(Book in) {
        if (in == null || in.getBid() == null) {
            return false;
        }
        in.setBorrowBy(null);
        return bookDao.updateBorrow(in);
    }

    @Override
    public boolean borrowBook(Book in) {
        if (in == null || in.getBid() == null || in.getBorrowBy() == null) {
            return false;
        }
        return bookDao.updateBorrow(in);
    }

    @Override
    public ResultEntity<Book> listBooks(int page, int row) {
        // 计算分页
        int offset = page==0?0:(page-1)*row;
        int count = bookDao.listBooksCount();
        ResultEntity<Book> res = new ResultEntity<>();
        res.setPage(page);
        res.setRow(row);
        // 数据越界
        if (offset > count){
            res.setData(null);
            return res;
        }
        res.setCount(count);
        res.setRecords((int) Math.ceil((double) count / (double) row));
        res.setData(bookDao.listBooks(row,offset));
        return res;
    }

    @Override
    public ResultEntity<Book> listBookById(int bid, int page, int row) {
        // 计算分页
        int offset = page==0?0:(page-1)*row;
        ResultEntity<Book> res = new ResultEntity<>();
        res.setPage(page);
        res.setRow(row);
        // 只能查出来一本
        List<Book> books = bookDao.listBookById(bid, row, offset);
        if (books==null || books.size()!=1){
            res.setData(null);
            res.setCount(0);
            res.setRecords(0);
        }else{
            res.setData(books);
            res.setCount(1);
            res.setRecords(1);
        }
        return res;
    }

    @Override
    public ResultEntity<Book> listBorrowBooks(int uid,int page, int row) {
        // 计算分页
        int offset = page==0?0:(page-1)*row;
        int count = bookDao.listBorrowBooksCount(uid);
        ResultEntity<Book> res = new ResultEntity<>();
        res.setPage(page);
        res.setRow(row);
        // 数据越界
        if (offset > count){
            res.setData(null);
            return res;
        }
        res.setCount(count);
        res.setRecords((int) Math.ceil((double) count / (double) row));
        res.setData(bookDao.listBorrowBooks(uid,row,offset));
        return res;
    }

    @Override
    public ResultEntity<Book> listBooksByConditions(Map<String, String> conditions, boolean like,int page,int row) {
        // 计算分页
        int offset = page==0?0:(page-1)*row;
        int count = bookDao.listBooksByStringConditionCount(conditions, like);
        ResultEntity<Book> res = new ResultEntity<>();
        res.setPage(page);
        res.setRow(row);
        // 数据越界
        if (offset > count){
            res.setData(null);
            return res;
        }
        res.setRecords((int) Math.ceil((double) count / (double) row));
        res.setData(bookDao.listBooksByStringCondition(conditions,like,row,offset));
        return res;
    }
}
